// pages/chargemen/chargemen.js
import urls from '../../utils/urls.js'
import loginFn from '../../utils/login.js'
const http = require('../../utils/util.js')
var app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		sharetip: false, //分享弹窗
		menCurrent: 0,
		classLists: [{
			name: '待审核',
			nums: '0',
			list: [],
			status: 0,
			sendStatus: false
		}, {
			name: '在职',
			nums: '0',
			list: [],
			status: 3,
			sendStatus: false
		}, {
			name: '离职',
			nums: "0",
			list: [],
			status: 5,
			sendStatus: false
		}, ],
		schoolId: ''

	},
	//获取所有老师信息
	teacher: function() {
		let that = this;
		let classLists = that.data.classLists;
		let index = that.data.menCurrent;
		app.request({
			url: 'school/teacher',
			data: {
				school_id: that.data.schoolId,
				type: 'admin',
				status: classLists[index]['status']
			},
			sCallback: (res) => {
				if(res.code == 1) {
					classLists[index]['list'] = res.data;
					classLists[index]['sendStatus'] = true;
					that.setData({
						classLists: classLists
					})
				} else {
					app.alert(res.msg,() => {
						wx.navigateBack();
					})
				}
			}
		})
	},
	//切换状态
	mymen: function(e) {
		var index = e.currentTarget.dataset.index;
		this.setData({
			menCurrent: index
		})

		if(!this.data.classLists[index]['sendStatus']) {
			this.teacher();
		}
	},
	// 老師詳情
	goteacherDetail: function(e) {
		let id = e.currentTarget.dataset.id
		wx.navigateTo({
			url: '/pages/chargeteacher/chargeteacher?teacherId=' + id +'&schoolId='+this.data.schoolId,
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log('options:',options);
		this.setData({
			schoolId: options.schoolId
		})
		
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		this.setData({
			classLists: [{
			name: '待审核',
			nums: '0',
			list: [],
			status: 0
		}, {
			name: '在职',
			nums: '0',
			list: [],
			status: 3,
		}, {
			name: '离职',
			nums: "0",
			list: [],
			status: 5
		} ]
		});
		this.getStatusNumber();
		this.teacher();
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function(res) {
		var pages = getCurrentPages()
		var prepages = pages[pages.length - 2]
		var img = prepages.data.schoolData.logoUrl;
		let path = 'pages/invited/invited?schoolId=' + this.data.schoolId
		return {
			title: '邀请你加入我们的学校',
			imageUrl: img,
			path: path,
			success: function(res) {
				wx.showToast({
					title: '分享成功'
				});
				// console.log('转发成功')
			},
			fail: function(res) {
				console.log('转发失败')
			}
		}
		this.setData({
			sharetip: false
		})
	},


	//获取不同状态数量
	getStatusNumber(){
		let that = this;
		let loginInfo = loginFn();
		loginInfo.schoolId = this.data.schoolId;
		wx.request({
			url: urls.teacherSummary,
			data: loginInfo,
			success: function(res) {
				if(res.data.code == '0' && res.data.data.length > 0) {
					let resData = res.data.data;
					let classLists = that.data.classLists;
					resData.forEach((item) => {
						classLists.forEach((detail) => {
							if(item.statusSummary == detail.status){
								detail.nums = item.statusCount;
							}
						})
					})

					that.setData({
						classLists: classLists
					});
				}
			}
		});
	}
})
